package cn.agiledata.bank.myspace.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.PeerInnerAccount;
import cn.agiledata.bank.myspace.bean.PeerInnerAccountBean;
import cn.agiledata.bank.myspace.bean.PeerInnerAccountQueryBean;
import cn.agiledata.bank.myspace.form.PeerInnerAccountQueryForm;
import cn.agiledata.bank.myspace.service.PeerAccountService;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class PeerInnerAccountAction extends IPBBaseAction implements Constant {
	/**
	 * 得到service
	 * @return
	 */
	public PeerAccountService getPeerAccountService() {
		return (PeerAccountService)IPBAppFactory.getObj(ForeDict.SERVICE_PEER_ACCOUNT);
	}
	/**
	 * 得到关联账户列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getPeerInnerAccountList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		PeerAccountService service = getPeerAccountService();
		PeerInnerAccountQueryForm form = (PeerInnerAccountQueryForm)actionForm;
		PeerInnerAccountQueryBean bean = new PeerInnerAccountQueryBean();
		bean.setAccountId(new Long(WeatherUtil.getAccountIdFromSession(request)));
		bean.setPageRecord(String.valueOf(Constant.PAGESIZE));
		if(StringUtil.isStrEmpty(form.getStartPage())) {
			bean.setStartPage("1");
		} else {
			bean.setStartPage(form.getStartPage());
		}
		
		PageBean pb = service.getPeerInnerAccountList(bean);
		request.setAttribute("list", pb);
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 链接到添加关联账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward toPeerInnerAccountAdd(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 添加关联账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward peerInnerAccountAdd(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		PeerInnerAccountBean bean = new PeerInnerAccountBean();
		BeanUtils.copyProperties(bean, actionForm);
		bean.setAccountId(new Long(WeatherUtil.getAccountIdFromSession(request)));
		PeerAccountService service = getPeerAccountService();
		service.peerInnerAccountAdd(bean);
		super.saveMessage(request,"peer_inner_account_add_success",null);
		request.setAttribute("back",request.getContextPath()+mapping.findForwardConfig("back").getPath());
		return mapping.findForward(FORWARD_GLOBAL_SUCCESS);
	}
	
	/**
	 * 链接到修改关联账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getPeerInnerAccount(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		PeerAccountService service = getPeerAccountService();
		PeerInnerAccount p = (PeerInnerAccount)service.getPeerInnerAccount(Long.parseLong(id));
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		// REMARK : MODIFY BY WUBO 20080916
		if (!p.getAccount().getId().equals(new Long(accountId))){
			p = null;
		}
		request.setAttribute("peerInnerAccount", p);
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 修改关联账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward peerInnerAccountModify(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		PeerInnerAccountBean bean = new PeerInnerAccountBean();
		BeanUtils.copyProperties(bean, actionForm);
		PeerAccountService service = getPeerAccountService();
		service.peerInnerAccountModify(bean);
		super.saveMessage(request,"peer_inner_account_modify_success",null);
		request.setAttribute("back",request.getContextPath()+mapping.findForwardConfig("back").getPath());
		return mapping.findForward(FORWARD_GLOBAL_SUCCESS);
	}
	/**
	 * 删除关联账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward peerInnerAccountDelete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		PeerAccountService service = getPeerAccountService();
		service.peerInnerAccountDelete(id);
		return mapping.findForward(SUCCESS);
	}

	
	

}
